#include "iterate.h"

enum {
	PATH_COLUMN,
	NUM_LINES_WHITESPACE,
	NUM_LINES_COMMENTS,
	NUM_LINES_CODE,
	N_COLUMNS
};

//---Public functions---------------------------------------------------------//

Iterate::Iterate(const char *projectPath, int c) {
	if (c == 'n') {
		_project = new ProjectFile(projectPath);
	} else if (c == 'o') {
		_project = new ProjectFile(projectPath);
		_project->load();
	}
	
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(window), "Iterate - New Project");
	g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(delete_event),
		NULL);
	gtk_container_set_border_width(GTK_CONTAINER(window), 3);
	mainBox = gtk_vbox_new(FALSE, 0);
	gtk_container_add(GTK_CONTAINER(window), mainBox);
	
	menuBar = gtk_menu_bar_new();
	gtk_box_pack_start(GTK_BOX(mainBox), menuBar, FALSE, FALSE, 0);
	menuItemProject = gtk_menu_item_new_with_label("Project");
	gtk_widget_show(menuItemProject);
	menuItemHelp = gtk_menu_item_new_with_label("Help");
	gtk_widget_show(menuItemHelp);
	gtk_menu_bar_append(menuBar, menuItemProject);
	gtk_menu_bar_append(menuBar, menuItemHelp);
	gtk_widget_show(menuBar);
	
	toolBar = gtk_toolbar_new();
	gtk_box_pack_start(GTK_BOX(mainBox), toolBar, FALSE, FALSE, 0);
	GtkWidget *button = gtk_button_new_with_label("Iterate!");
	gtk_toolbar_append_widget(GTK_TOOLBAR(toolBar), button,
		"Iterate through this project", "");
	gtk_widget_show(button);
	gtk_widget_show(toolBar);
	
	statusBar = gtk_statusbar_new();
	gtk_box_pack_end(GTK_BOX(mainBox), statusBar, FALSE, FALSE, 0);
	gtk_widget_show(statusBar);
	
	initializeFileList();
	
	gtk_widget_show(mainBox);
	gtk_widget_show(window);
}

gboolean Iterate::delete_event(GtkWidget* widget, GdkEvent *event, gpointer data) {
	gtk_main_quit();
	return 0;
}

void Iterate::addFile(const char *filename) {
	_project->addFile(filename);
	fileMetric metric = {(char *)filename, 10, 25, 63};
	appendToFileList(&metric);
}

void Iterate::saveProject(void) {
	_project->save();
}

void Iterate::printProjectInfo(void) const {
	_project->print();
}

//---Protected functions------------------------------------------------------//

void Iterate::initializeFileList(void) {
	GtkTreeViewColumn *column;
	GtkCellRenderer *renderer;
	GtkListStore *fileList; 
	
	fileList = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT,
		G_TYPE_INT);
	fileListView = gtk_tree_view_new_with_model(GTK_TREE_MODEL(fileList));
	
	renderer = gtk_cell_renderer_text_new();
	g_object_set(G_OBJECT(renderer), "foreground", "blue", NULL);
	column = gtk_tree_view_column_new_with_attributes(
		"Path", renderer, "text", PATH_COLUMN, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(fileListView), column);
	
	renderer = gtk_cell_renderer_text_new();
	column = gtk_tree_view_column_new_with_attributes("lines of whitespace", renderer,
		"text", NUM_LINES_WHITESPACE, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(fileListView), column);
	
	renderer = gtk_cell_renderer_text_new();
	column = gtk_tree_view_column_new_with_attributes("lines of comments", renderer,
		"text", NUM_LINES_COMMENTS, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(fileListView), column);
	
	renderer = gtk_cell_renderer_text_new();
	column = gtk_tree_view_column_new_with_attributes("lines of code", renderer,
		"text", NUM_LINES_CODE, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(fileListView), column);
	
	gtk_box_pack_start(GTK_BOX(mainBox), fileListView, TRUE, TRUE, 0);
	gtk_widget_show(fileListView);
	
	g_object_unref(fileList);
}

void Iterate::appendToFileList(const fileMetric *metric) {
	GtkListStore *store;
	GtkTreeIter iter;
	
	store = GTK_LIST_STORE(gtk_tree_view_get_model(
		GTK_TREE_VIEW(fileListView)));
	
	gtk_list_store_append(store, &iter);
	gtk_list_store_set(store, &iter, 
			PATH_COLUMN, metric->filepath,
			NUM_LINES_WHITESPACE, metric->whitespaceLines,
			NUM_LINES_COMMENTS, metric->commentLines,
			NUM_LINES_CODE, metric->sourceLines, -1);
}
